using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using Rensoft;
using System.Windows.Forms;

namespace Rensoft.CRM.Client
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        protected override void OnAfterInstall(System.Collections.IDictionary savedState)
        {
            base.OnAfterInstall(savedState);
            applySettings();
        }

        private void applySettings()
        {
            try
            {
                ConfigHelper config = new ConfigHelper(
                    Context.Parameters["assemblypath"],
                    "Rensoft.CRM.Client.Properties.Settings");

                config.Settings["Rensoft_CRM_Client_AccountsAPI_AccountsService"] = Context.Parameters["AccountsService"];
                config.Settings["Rensoft_CRM_Client_HostingAPI_HostingService"] = Context.Parameters["HostingService"];

                config.Save();
            }
            catch (Exception ex)
            {
                DialogResult result = MessageBox.Show(
                    ex.Message, "Installer Error",
                    MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Error);

                if (result == DialogResult.Retry)
                {
                    applySettings();
                }
            }
        }
    }
}